﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;

using Pr0ject0.Models;
using Pr0ject0.ViewModel;

namespace Pr0ject0.Controllers
{
    [Authorize(Roles="Hiring Manager")]
    public class HiringManagerController : Controller
    {
        JobModelDB jobs = JobModelDB.GetInstance();
        HiringManagerDb managers = new HiringManagerDb();
        JobApplicationDB applications = JobApplicationDB.GetInstance();

        public ActionResult Index()
        {
            ManagerIndexModel model = managers.GetIndexModel(Membership.GetUser().UserName);
            return View(model);
        }

        public ActionResult Jobs()
        {
            List<JobViewModel> jobList = managers.GetJobList(Membership.GetUser().UserName);
            return View(jobList);
        }

        [HttpPost]
        public ActionResult Jobs(FormCollection collection)
        {
            List<int> jobIds = new List<int>();
            foreach (string key in collection.Keys)
            {
                if (collection[key].Contains("true"))
                {
                    jobIds.Add(Convert.ToInt32(key));
                }
            }

            if (jobIds.Count > 0)
            {
                JobModelDB.GetInstance().CloseJobs(jobIds.ToArray());
            }

            return Redirect("Jobs");
        }

        public ActionResult AddJob()
        {
            JobModel job = managers.GetNewJobModel(Membership.GetUser().UserName);
            return View(job);
        }

        [HttpPost]
        public ActionResult AddJob(JobModel job)
        {
            jobs.addJobs(job);
            return RedirectToAction("Jobs");
        }

        public ActionResult Interviews()
        {
            return View(managers.GetInterviewList(Membership.GetUser().UserName));
        }

        [HttpPost]
        public ActionResult Interviews(FormCollection collection)
        {
            foreach (string key in collection.Keys)
            {
                if (collection[key].Contains("true"))
                {
                    JobApplicationDB.GetInstance().ChangeStatus(
                        Membership.GetUser().UserName, Convert.ToInt32(key), 1);
                }
            }
            return RedirectToAction("Interviews", new { });
        }

        public ActionResult Details(int id)
        {
            JobApplicationModel model = managers.GetApplication(
                Membership.GetUser().UserName, id);
            
            if (model == null)
            {
                return RedirectToAction("Interviews");
            }
            else
            {
                return View(model);
            }
        }

        [HttpPost]
        public ActionResult Details(int id, string str)
        {
            int current = applications.jobApplications.Find(x => x.ID == id).status;
            string ret = "Index";

            if (current == 4)
            {
                ret = "Interviews";
            }
            else if (current == 6)
            {
                ret = "Hire";
            }

            if (current == 4 || current == 6)
            {
                if (str == "accept")
                {
                    applications.ChangeStatus(Membership.GetUser().UserName, id, current + 1);
                }
                else if (str == "reject")
                {
                    applications.ChangeStatus(Membership.GetUser().UserName, id, 1);
                }
                else if (str == "postpone")
                {
                    applications.ChangeStatus(Membership.GetUser().UserName, id, 2);
                }
            }
            

            return RedirectToAction(ret);
        }

        public ActionResult Hire()
        {
            ViewBag.Actions = new string[] { "", "Accept", "Reject", "Postpone" };
            return View(managers.GetHireList(Membership.GetUser().UserName));
        }

        [HttpPost]
        public ActionResult Hire(FormCollection collection)
        {
            string action = collection["str"];
            collection.Remove("str");
            List<int> appIds = new List<int>();
            int newStatus = -1;

            foreach (string key in collection.Keys)
            {
                if (collection[key].Contains("true"))
                {
                    appIds.Add(Convert.ToInt32(key));
                }
            }

            if (action == "Accept")
            {
                newStatus = 7;
            }
            else if (action == "Reject")
            {
                newStatus = 1;
            }
            else if (action == "Postpone")
            {
                newStatus = 2;
            }

            if (appIds.Count > 0)
            {
                if (newStatus > 0)
                {
                    foreach (int id in appIds)
                    {
                        JobApplicationDB.GetInstance().ChangeStatus(Membership.GetUser().UserName, id, newStatus);
                    }
                }
            }

            return RedirectToAction("Hire");
        }

    }
}
